var eventEditor = new function() {
    const DAYMONTH_MSG = "day {1} month {2} ({3} calendar)";
	var vieCalendar;
    var event;
	var daymonthControl;
    var titleControl;
    var descriptionControl;
    var yearGroupControl;
    var specialDayControl;
    
	this.initialize = function() {
        vieCalendar = window.arguments[0];
		event = window.arguments[1];
        
		daymonthControl     = document.getElementById("daymonth");
        titleControl        = document.getElementById("title");
        descriptionControl  = document.getElementById("description");
		yearGroupControl    = document.getElementById("year-group");
		specialDayControl   = document.getElementById("special-day");
        
        titleControl.value = event.title || "";
        descriptionControl.value = event.description || "";
        yearGroupControl.addEventListener("RadioStateChange", this.changeYearType, true);
        yearGroupControl.selectedIndex = (event.year)? 0 : (event.type == SOLAR)? 1 : 2;
        specialDayControl.checked = event.special;
                
        this.changeYearType();
	};

	this.acceptData = function() {
		var yeartype = yearGroupControl.selectedIndex;
		
        event.day = (yeartype != 2)? vieCalendar.getSolarDay() : vieCalendar.getLunarDay();
        event.month = (yeartype != 2)? vieCalendar.getSolarMonth() : vieCalendar.getLunarMonth();
        event.year = (yeartype == 0)? vieCalendar.getSolarYear() : null;
        event.type = (yeartype != 2)? SOLAR : LUNAR;
        event.title = titleControl.value;
        event.description = descriptionControl.value;
        event.special = specialDayControl.checked;
        
        var storage = new Storage(getDbFile());
        if(!event.id) {
            storage.insertEvent(event);
        } else {
            storage.modifyEvent(event);
        }
        
        return true;
	};
    
    this.changeYearType = function() {
        switch(yearGroupControl.selectedIndex) {
            case 0: case 1:
                daymonthControl.value = formatString(DAYMONTH_MSG, vieCalendar.getSolarDay(), vieCalendar.getSolarMonth(), SOLAR);
                break;
            case 2:
                daymonthControl.value = formatString(DAYMONTH_MSG, vieCalendar.getLunarDay(), vieCalendar.getLunarMonth(), LUNAR);            
                break;
        }
    }
}